/*
Naijanimi Free SMS Application for Nokia mobiles.

"Naijanimi Free SMS Application" is protected by the copyright law
(c) 2011 Enrico Miglino / Tech Consulting (enrico.miglino@ovi.com)
Distributed under the LGPL 3.0 license.

This file is part of "Naijanimi Free SMS Application".

    "Naijanimi Free SMS Application" is free software: you can redistribute it
    and/or modify it under the terms of the Lesser GNU General Public License
    as published by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Naijanimi Free SMS Application" is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with "Naijanimi Free SMS Application".
    If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.0

// Generic On/Off switch to set status variables
Item {
    id: toggleswitch

    width: background.width;
    height: background.height
    // Initial switch status
    property bool on: false

    // This function changes the state of the switch
    function toggle() {
        if (toggleswitch.state == "on")
            toggleswitch.state = "off";
        else
            toggleswitch.state = "on";
    }

    function releaseSwitch() {
        if (knob.x == 1) {
            if (toggleswitch.state == "off") return;
        }
        if (knob.x == 78) {
            if (toggleswitch.state == "on") return;
        }
        toggle();
    }

    // Background switch image
    Image {
        id: background
        source: "images/Switchbackground.svg"
        MouseArea {
            anchors.fill: parent
            onClicked: toggle()
        }
    }

    // Know switch component
    Image {
        id: knob
        x: 1; y: 2
        source: "images/knob.svg"

        MouseArea {
            anchors.fill: parent
            drag.target: knob;
            drag.axis: Drag.XAxis;
            drag.minimumX: 1;
            drag.maximumX: 78
            onClicked: toggle()
            onReleased: releaseSwitch()
        }
    }

    states: [
        State {
            name: "on"
            PropertyChanges { target: knob; x: 78 }
            PropertyChanges { target: toggleswitch; on: true }
        },
        State {
            name: "off"
            PropertyChanges { target: knob; x: 1 }
            PropertyChanges { target: toggleswitch; on: false }
        }
    ]

    transitions: Transition {
        NumberAnimation { properties: "x"; easing.type: Easing.InOutQuad; duration: 200 }
    }
}
